package com.focusai.efairy.model.request;

import com.focusai.efairy.model.manager.CacheManager;
import com.focusai.efairy.model.user.UpLoadUserInfo;
import com.focusai.efairy.network.Response;
import com.focusai.efairy.network.exception.ParseException;
import com.focusai.efairy.network.request.base.PutPureJSONRequest;
import com.focusai.efairy.network.utils.UrlUtils;
import com.focusai.efairy.utils.GsonHelper;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by luo_guangwen on 2017/8/13.
 */

public class ChangUserInfoRequest extends PutPureJSONRequest<Integer> {

    private UpLoadUserInfo upLoadUserInfo;
    public ChangUserInfoRequest(UpLoadUserInfo upLoadUserInfo,Response.Listener<Integer> listener) {
        super(UrlUtils.createUrl("/appapi/user_center"), listener);
        this.upLoadUserInfo = upLoadUserInfo;
    }

    @Override
    protected Integer parse(String result) throws ParseException {
        try {
            JSONObject jsonObject = new JSONObject(result);
            return jsonObject.getInt("err_code");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public String getPureJSON() throws JSONException {
       if (this.upLoadUserInfo != null){
           this.upLoadUserInfo.setAccess_token(CacheManager.getAccessToken());
           this.upLoadUserInfo.setEfairyuser_id(CacheManager.getUserId());
           return GsonHelper.getInstance().toJson(upLoadUserInfo);
       }
        return "";
    }

    @Override
    protected boolean handleRawResultData() {
        return true;
    }

}
